<?php
class Bbling_NewsController extends Evebit_Controller_Action_Backend {
	
	private $_module = 'bbling';
	
	private $_controller = 'news';
	
	public function indexAction() {
		$session = $this->getSession();
		unset ( $session->grids ['filters'] );
		$session->initGrid ( 'news', 
			array ('id' => array ('fields' => 'id', 'type' => 'int'), 
			       'content' => array ('fields' => 'content', 'type' => 'string' ), 
			       'select_published' => array ('fields' => 'select_published', 'type' => 'string' ),
			       'order'=>array('fields'=>'order','type'=>'string'),
			       'link' => array ('fields' => 'link', 'type' => 'string' )),
			array ('id' => 'id', 'content' => 'content', 'select_published' => 'select_published', 'order' => 'order' ),
			array ('size' => 10, 'sort' => 'id', 'order' => 'desc' ) 
		);
		$stateEntity = new Bbling_Model_Entity_News();		
		/*var_dump($stateEntity->getAllNews());
		exit;*/
		$collection = $stateEntity->getCollection();
		$adapter = new Evebit_Model_Paginator_Adapter ( $collection );
		$paginator = new Zend_Paginator ( $adapter );
		$session->applyCollection ( $collection, $paginator );
		
		$this->view->paginator = $paginator;
		$this->view->module = $this->_module;
		$this->view->controller = $this->_controller;
		$this->view->state = $session->getGridState();
		
	}

	
    public function addAction() {
		$this->_forward ( 'edit', null, null, array ('id' => 0 ) );
	}
	
	public function editAction(){
		$id = $this->getRequest()->getParam('id');
		
		$entity = new Bbling_Model_Entity_News();
		$entity->load($id);
		
		$title = $entity->id?'Edit '.ucfirst($this->_controller):'Add new '.ucfirst($this->_controller);
		
		$this->view->entity = $entity;
		$this->view->title = $title;
		$this->view->module = $this->_module;
		$this->view->controller = $this->_controller;
		$this->view->fieldsets = $entity->getFormField('news_list');
	}
	
    public function saveAction() {
		$entity = new Bbling_Model_Entity_News();
		$data = $this->getRequest()->getPost('data');
		$entity->addData($data);
		if (!$entity->id && $entity->id !== 0){
			$entity->id = null;
		}
		try {
			$entity->save();
		} catch (Exception $e) {
			$this->getSession()->addError($this->__('Save failed'));
			return $this->_forward('edit', $this->_controller, $this->_module, array("id"=> $data["id"]));
		}
		$this->getSession()->addSuccess($this->__('Save Success'));
		$this->_redirect('/'.$this->_module.'/'.$this->_controller);
	}
	
    public function deleteAction() {
		$ids = $this->getRequest()->getParam('id');
		if (count($ids) > 0) {
			foreach ($ids as $id) {
				try {
					$entity = new Bbling_Model_Entity_News();
					$entity->load($id);
					if ($entity->getId() != $id) {
						throw new Exception("");
					}
					$entity->delete();
				} catch (Exception $e) {
					$this->getSession()->addError(
						$this->__("User %d not found or deleted failed", $id));
				}
			}
			$this->getSession()->addSuccess($this->__('Delete Success'));
		} else {
			$this->getSession()->addNotice($this->__('Please select a record'));
		}
		$this->_redirect('/'.$this->_module.'/'.$this->_controller);
	}
	
    public function resetAction() {
		$this->getSession()->resetGrid();
		$this->_forward('index', $this->_controller);
	}
}